package com.common.library.ui.view.ptr;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * 重写上拉下拉控件
 *
 * @note Created by Chaosxing on 2016/10/20.
 */

public class PtrDefaultLayout extends com.chanven.lib.cptr.PtrFrameLayout {
    public PtrDefaultLayout(Context context) {
        super(context);
        initViews();
    }

    public PtrDefaultLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public PtrDefaultLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    float downX;
    float downY;
    private boolean leftORright, topORbottom;//当前滑动方向
    private float minMove = 40;//最小滑动距离

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float startX = event.getX();
        float startY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                Logger.i("按下");
                downX = startX;
                downY = startY;
                break;
            case MotionEvent.ACTION_MOVE:
//                Logger.i("移动");
                if (Math.abs(downX - startX) > minMove && !leftORright) {   //左右滑动
//                    Logger.i("左右滑动");
                    topORbottom = true;
                    minMove = 0;
                    return dispatchTouchEventSupper(event);
                } else if (Math.abs(downY - startY) > minMove && !topORbottom) {   //上下滑动
//                    Logger.i("上下滑动");
                    leftORright = true;
                    minMove = 0;
                    return super.dispatchTouchEvent(event);
                }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
//                Logger.i("抬起");
                topORbottom = leftORright = false;
                minMove = 40;
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    private void initViews() {
        if (isInEditMode()) {
            return;
        }
        PtrDefaultHeader mPtrClassicHeader = new PtrDefaultHeader(getContext());
        setHeaderView(mPtrClassicHeader);
        addPtrUIHandler(mPtrClassicHeader);

        PtrDefaultFooter loadMoreViewFactory = new PtrDefaultFooter();
        setFooterView(loadMoreViewFactory);

        //推荐参数配置
        setDurationToClose(200);
        setDurationToCloseHeader(500);
        setRatioOfHeaderHeightToRefresh(1.2f);
        setResistance(2.5f);

    }

}